﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.Configuration;
using System.IO;

public partial class ShowPPT : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Filename"] != null && !string.IsNullOrEmpty(Request.QueryString["Filename"]))
        {
            if (Request.QueryString["download"] != null && !string.IsNullOrEmpty(Request.QueryString["download"]))
            {
                SaveFile(Request.QueryString["Filename"]);
            }
            else
            {
                string filepath = ConfigurationManager.AppSettings["strHttp"] + "Presentation/" + Request.QueryString["Filename"];
                hdpresentation.Value = "http://docs.google.com/gview?url=" + filepath + "&amp;embedded=true";
            }


            // DownloadFile(filepath);
        }
    }



    private void SaveFile(string filename)
    {
        string FilePath = Server.MapPath("~/Presentation/" + Request.QueryString["Filename"]);
        if (!string.IsNullOrEmpty(FilePath))
        {
            var fInfo = new FileInfo(FilePath);

            //Response.Buffer = false;
            Response.Clear();

            if (!fInfo.Exists)
            {
                Response.Write("File {" + fInfo.Name + "} does not exist!");
            }
            else
            {
                Response.ContentType = "application/vnd.ms-powerpoint";
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fInfo.Name + "\"");
                Response.AddHeader("Content-Length", fInfo.Length.ToString());
                Response.TransmitFile(fInfo.FullName);
            }
        }
    }

    //private void WriteFileData(EJData ejdata)
    //{
    //    if (ejdata != null && ejdata.FormattedData != null)
    //    {
    //        Response.Clear();
    //        Response.ContentType = "text/plain";
    //        Response.AddHeader("X-Content-Type-Options", "nosniff"); // added for IE-9 File download

    //        //if (Config.ShowEJInTextEditor)
    //        //    Response.AddHeader("content-disposition", "attachment; filename=\"EJ.txt\"");

    //        Response.Write("------------------------------------------------------");
    //        Response.Write("\r\n");
    //        Response.Write("EJ File for terminal: " + ejdata.TerminalName + " for the Date: " + ejdata.ForDate);
    //        Response.Write("\r\n");
    //        Response.Write("------------------------------------------------------");
    //        Response.Write("\r\n");
    //        Response.Write("\r\n");
    //        Response.Write("\r\n");

    //        try
    //        {
    //            Response.BinaryWrite(ejdata.FormattedData);
    //        }
    //        catch (Exception ex)
    //        {

    //        }
    //    }
    //    else
    //    {
    //        Response.Clear();
    //        Response.ContentType = "text/plain";
    //        Response.Write("No EJ file exists for the terminal: " + ejdata.TerminalName);
    //        Response.Flush();
    //    }
    //}



    //private void DownloadpptFiles(EJData ejdata)
    //{
    //    if (ejdata != null && ejdata.FormattedData != null && !string.IsNullOrEmpty(ejdata.FilePath))
    //    {
    //        string fileName = ejdata.FilePath;
    //        Response.Clear();
    //        Response.Buffer = true;
    //        //Response.ClearContent();
    //        //Response.ClearHeaders();
    //        //Response.ContentType = "text/plain";
    //        Response.AddHeader("Content-Type", "text/plain");
    //        Response.AddHeader("content-disposition", "attachment; filename=" + fileName);

    //        //  Response.AddHeader("Content-Length", ejdata.FormattedData.Length.ToString());
    //        try
    //        {
    //            Response.BinaryWrite(ejdata.FormattedData);
    //            Response.Flush();
    //            //Response.Close();
    //            // Response.End();
    //        }
    //        catch (Exception ex)
    //        {

    //        }
    //    }
    //}




    private static void DownloadFile(string FilePath)
    {

        Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
        ppApp.Visible = MsoTriState.msoTrue;
        Presentations ppPresens = ppApp.Presentations; Presentation objPres = ppPresens.Open("d:\\Copy of ecovillage 3 ppt.ppt", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
        Slides objSlides = objPres.Slides;
        Microsoft.Office.Interop.PowerPoint.SlideShowWindows objSSWs; Microsoft.Office.Interop.PowerPoint.SlideShowSettings objSSS;
        //Run the Slide show
        objSSS = objPres.SlideShowSettings;
        objSSS.Run();
        objSSWs = ppApp.SlideShowWindows;
        while (objSSWs.Count >= 1)
            System.Threading.Thread.Sleep(100);
        //Close the presentation without saving changes and quit PowerPoint
        objPres.Close();
        ppApp.Quit();
    }
}